const fs = require('fs')
const xlsx = require('node-xlsx')
const { shell } = require('electron')
// const PDFParser = require("pdf2json");
  /* 文件夹操作 */
export default {

  // 是否存在？
  exist (dir) {
    return new Promise((resolve, reject) => {
      fs.access(dir, function (error) {
        if (error == null) {
          resolve(error)
        } else {
          reject(error)
        }
      })
    })
  },
  // 浏览文件夹
  readDir (dir) {
    return new Promise((resolve, reject) => {
      fs.readdir(dir, (err, data) => {
        if (err == null) {
          resolve(data)
        } else {
          reject(err)
          // console.log(err)
        }
      })
    })
  },

  // 创建文件夹
  addDir (dir) {
    return new Promise(resolve => {
      fs.mkdir(dir, err => {
        if (err) {
          // console.log(err)
          resolve()
        } else {
          resolve(err)
        }
      })
    })
  },

  // 改变文件/文件夹
  rename (oldDir, newDir) {
    console.log(oldDir, newDir)
    return new Promise(resolve => {
      fs.rename(oldDir, newDir, err => {
        if (err == null) {
          resolve()
        } else {
          // console.log(err)
        }
      })
    })
  },

  // 删除文件夹
  delDir (dir) {
    return new Promise(resolve => {
      fs.rmdir(dir, err => {
        resolve(err)
      })
    })
  },

  /* 文件操作 */

  // 读文件
  readFile (dir) {
    return new Promise(resolve => {
      fs.readFile(dir, 'utf8', (err, data) => {
        if (err == null) {
          resolve(data)
        } else {
          resolve()
          // console.log(err)
        }
      })
    })
  },

  stat(dir) {
    return new Promise(resolve => {
      fs.stat(dir, (err, data) => {
        if (err == null) {
          resolve(data)
        } else {
          resolve()
        }
      })
    })
  },

  // 新建文件
  writeFile (dir, content) {
    return new Promise(resolve => {
      fs.writeFile(dir, content, error => {
        resolve(error)
      })
    })
  },

  // 写入文件
  appendFile (dir, content) {
    return new Promise(resolve => {
      fs.appendFile(dir, content, err => {
        resolve(err)
      })
    })
  },

  // 删文件
  delFile (dir) {
    return new Promise(resolve => {
      fs.unlink(dir, err => {
        resolve(err)
      })
    })
  },

  /* 表格 */

  // 读表格
  readXlsx (dir) {
    return new Promise(resolve => {
      this.exist(dir).then(() => {
        let data = xlsx.parse(dir)
        resolve(data)
      })
    })
  },

  // 创建表格
  buildXlsx (dir, data) {
    this.exist(dir).then(() => {
      fs.writeFileSync(dir, xlsx.build(data), 'binary')
    })
  },

  // 修改表格
  delXlsx (dir) {
    this.delFile(dir)
  },

  // 新表格
  newXlsx (dir, sheetName, data) {
    this.exist(dir).then(() => {
      // console.log(dir, sheetName, data)
      this.buildXlsx(dir, [
        {
          name: sheetName,
          data
        }
      ])
      // console.log('导入表格成功！')
    })
  },

  // 插入表格
  oldXlsx (dir, sheetName, data) {
    this.readXlsx(dir).then(res => {
      // console.log(res)
      this.buildXlsx(dir, [
        ...res,
        {
          name: sheetName,
          data
        }
      ])
      // console.log('导入表格成功！')
    })
  },

  // 修改
  editXlsx (dir, data) {
    this.delXlsx(dir).then(() => {
      this.buildXlsx(dir, data)
    })
  },

  // 文件名称
  fileName (str, type) {
    let arr = str.split('\\')
    let filName = arr[arr.length - 1]
    return filName.replace(type, '')
  },

  // 文件大小
  fileSize (size) {
    let newSize = ''
    let num = 1024 * 1024
    if (size <= num) {
      newSize = (parseInt(size) / 1024).toFixed(1) + ' K '
    } else if (num < size <= 40 * num) {
      newSize = (parseInt(size) / 1024 / 1024).toFixed(1) + ' M '
    }
    return newSize
  },

  // 文件地址
  filedir (str) {
    let arr = str.split('\\')
    let filName = arr[arr.length - 1]
    return {
      dir: str.replace(filName, ''),
      sheetName: arr[arr.length - 2],
      folder: arr[arr.length - 3],
      dirname: arr[0] + '/' + arr[1],
      type: str.split('.')[1]
    }
  },

  // 打开目录
  openWeb (dir) {
    shell.openExternal(dir)
  },

  // 打开文件
  openFile (dir) {
    shell.openPath(dir)
  }

  // readPdf(dir) {
  // 	const pdfParser = new PDFParser(this, 1)

  // 	// let result = pdfParser.loadPDF(dir);
  // 	// console.log(dir, pdfParser)

  // 	pdfParser.on("pdfParser_dataError", errData =>
  // 		console.error(errData.parserError)
  // 	);
  // 	pdfParser.on("pdfParser_dataReady", pdfData => {
  // 		const data = pdfParser.getRawTextContent()
  // 		// console.log('文本信息:', data, pdfData)
  // 	});
  // }
}
